package com.sinodata.bsm.cicp.collector.ha;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.collector.Collector;
import com.sinodata.bsm.cicp.exception.CollectorException;
import com.sinodata.bsm.cicp.util.CollectorUtil;
import com.sinodata.bsm.common.vo.ResPropertyValue;

/**
 * 
 * <p>
 * Description: 双机的采集程序，采集子节点状态
 * </p>
 *
 * @author liulibo
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-12-26 AM 9:56:41    liulibo        1.0         To create
 * </p>
 *
 * @since 
 * @see
 */
public class HACollector extends Collector {
    private static final Logger logger = Logger.getLogger(HACollector.class);

    /**
     * 采集方法
     * 
     * @param ip 执行命令的主机ip地址
     * @return 资源列表
     * @throws IOException
     */
    @Override
    public List<ResPropertyValue> collect(String ip) throws CollectorException {
        List<ResPropertyValue> resProValList = null;
        BufferedReader reader = null;
        try {
            reader = CollectorUtil.getReader("clstat -o", ip);
            // 解析命令响应结构
            resProValList = this.parseReader(reader);
        } catch (IOException e) {
            logger.error("Failed to collector HA ", e);
            throw new CollectorException("Failed to collector HA ", e);
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    logger.error("Failed to close IO resource ", e);
                    throw new CollectorException("Failed to close IO Resource ", e);
                }
            }
        }
        return resProValList;
    }

    /**
     * 解析 响应结果
     * 
     * @param reader 响应结果
     * @return 资源列表
     * @throws IOException
     */
    private List<ResPropertyValue> parseReader(BufferedReader reader) throws IOException

    {
        List<ResPropertyValue> resProValList = null;
        // 匹配：State ： xxx Nodes : xxxx
        Pattern parntPatn = Pattern.compile("\\s*State\\s*:\\s*(\\S*)\\s*Nodes\\s*:\\s*(\\S*)\\s*");
        // 匹配： xx : yy
        Pattern subPatn = Pattern.compile(".*\\:\\s*(\\S*)\\s*");
        String[] propMetas = getPropMeta();
        String tmp = null;
        Matcher parntMach = null;
        Matcher subMach = null;
        resProValList = new ArrayList<ResPropertyValue>();
        ResPropertyValue propertyValueX = null;
        ResPropertyValue.PropValue[] propValues = null;
        while ((tmp = reader.readLine()) != null) {// 匹配了正则的数据才是目标数据
            parntMach = parntPatn.matcher(tmp);
            subMach = subPatn.matcher(tmp);
            if (parntMach.matches()) {
                propertyValueX = new ResPropertyValue();
                propValues = new ResPropertyValue.PropValue[propMetas.length];
                propValues[0] = ResPropertyValue.createPropValue(parntMach.group(1), propMetas[0]);
            } else if (subMach.matches()) {
                // 可以确保 下一行执行的代码不会报空指针异常，因为响应结果格式形式决定了先出现匹配
                // parntPatn,紧接着就匹配subPatn
                propValues[1] = ResPropertyValue.createPropValue(subMach.group(1),

                propMetas[1]);
                propertyValueX.setValues(propValues);
                resProValList.add(propertyValueX);
                break;// 目标数据只出现一次，所以得到后，马上break；
            }
        }
        return resProValList;
    }

    @Override
    public String[] getParamMeta() {
        return new String[] {};
    }

    @Override
    public String[] getPropMeta() {
        return new String[] { "State", "SubState" };
    }

}
